import Vue from 'vue'
import App from './App.vue'
import router from './router'

import axios from 'axios'
import VueAxios from 'vue-axios'
import { tokenUrl } from './valid/tokenUrl'

//注册axios, 这样就可以在组件里面使用



import 'lib-flexible/flexible'

import {
  Tabbar,
  TabbarItem,
  Search,
  Icon,
  Swipe,
  SwipeItem,
  Lazyload,
  Toast,
  Sidebar,
  SidebarItem,
  Tag,
  Form,
  Field,
  Button,
  NavBar,
  Popup,
  Stepper,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton,
  Checkbox,
  CheckboxGroup,
  SwipeCell,
  SubmitBar,
  Empty,
  AddressList,
  AddressEdit,
  Area,
  Tab,
  Tabs,
  Uploader,
  Cell,
  CellGroup,
  Image as VanImage,
  Notify,
  CountDown,
  Sticky,


} from 'vant';

Vue
  .use(Tabbar)
  .use(TabbarItem)
  .use(Search)
  .use(Icon)
  .use(Swipe)
  .use(SwipeItem)
  .use(Lazyload, {
    lazyComponent: true,
  })
  .use(Toast)
  .use(Sidebar)
  .use(SidebarItem)
  .use(Tag)
  .use(Form)
  .use(Field)
  .use(Button)
  .use(NavBar)
  .use(Popup)
  .use(Stepper)
  .use(GoodsAction)
  .use(GoodsActionButton)
  .use(GoodsActionIcon)
  .use(Checkbox)
  .use(CheckboxGroup)
  .use(SwipeCell)
  .use(SubmitBar)
  .use(Empty)
  .use(AddressList)
  .use(AddressEdit)
  .use(Area)
  .use(Tab)
  .use(Tabs)
  .use(Uploader)
  .use(Cell)
  .use(CellGroup)
  .use(VanImage)
  .use(Notify)
  .use(CountDown)
  .use(Sticky)

// 设置请求基础路径
axios.defaults.baseURL = 'http://www.kangliuyong.com:10002';

// 添加请求拦截器
axios.interceptors.request.use(config => {

  let tokenString = localStorage.getItem('token')

  let appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA='

  //处理请求的公共参数
  if (config.method === 'get') {
    config.params = config.params || {};
    config.params.appkey = appkey;

    if (tokenUrl.indexOf(config.url) > -1) {
      config.params.tokenString = tokenString;
    }

  } else if (config.method === 'post') {
    config.data = config.data || {};

    if (tokenUrl.indexOf(config.url) > -1) {
      config.data.tokenString = tokenString;
    }

    //序列化post请求参数
    let dataParams = '';
    for (let key in config.data) {
      dataParams += `${key}=${config.data[key]}&`
    }
    dataParams += `appkey=${appkey}`;

    config.data = dataParams;

  }

  Toast.loading({
    message: '加载中...',
    forbidClick: true,
    loadingType: 'spinner',
    // 不会自动关闭
    duration: 0
  });
  // 在发送请求之前做些什么
  return config;

}, err => {
  // 关闭提示
  Toast.clear()
  // 对请求错误做些什么
  return Promise.reject(err);
});

// 添加响应拦截器
axios.interceptors.response.use(response => {
  // 关闭提示
  Toast.clear()
  // 对响应数据做点什么
  return response;
}, err => {
  // 关闭提示
  Toast.clear()
  // 对响应错误做点什么
  return Promise.reject(err);
});

Vue.use(VueAxios, axios)
Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
